/**
 * 设置商户数据接口
 */

//引入Q模块
var Q = require('q');
//引入公共函数
var util = require('../../util');

//setStoreData接口入口文件
var index = function(getData, postData, config, _redis, db) {
	var deferred = Q.defer();
	if(postData.infoId && postData.infoVal) {
		let selectData = {};
		let updateData = {};
		selectData[postData.infoId] = /(.*)/;
		updateData[postData.infoId] = postData.infoVal;
		db.update('storeInfo', selectData, updateData).then(function() {
			return db.connectDB('common');
		}, function() {
			//更新商户数据库的商户资料失败
			deferred.reject('-0161');
		}).then(function(commonDB) {
			return commonDB.update('storeList', {storeId: postData.storeId}, updateData);
		}, function() {
			//连接到公共数据库失败
			deferred.reject('-0127');
		}).then(function() {
			deferred.resolve(1);
		}, function() {
			//更新公共数据库的商户资料失败
			deferred.reject('-0162');
		});
	}
	else {
		//设置商户资料接口参数不足
		deferred.reject('-0160');
	}
	return deferred.promise;
}

//暴露接口
module.exports = {
	index: index
}